package com.ruoyi.procurementmanagement.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.procurementmanagement.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.procurementmanagement.mapper.EsProcurementPlanMapper;
import com.ruoyi.procurementmanagement.service.IEsProcurementPlanService;

/**
 * 采购计划(es_procurement_plan)Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-21
 */
@Service
public class EsProcurementPlanServiceImpl implements IEsProcurementPlanService
{
    @Autowired
    private EsProcurementPlanMapper esProcurementPlanMapper;

    /**
     * 查询采购计划(es_procurement_plan)
     *
     * @param id 采购计划(es_procurement_plan)主键
     * @return 采购计划(es_procurement_plan)
     */
    @Override
    public EsProcurementPlan selectEsProcurementPlanById(Integer id)
    {
        return esProcurementPlanMapper.selectEsProcurementPlanById(id);
    }

    /**
     * 查询采购计划(es_procurement_plan)列表
     *
     * @param esProcurementPlan 采购计划(es_procurement_plan)
     * @return 采购计划(es_procurement_plan)
     */
    @Override
    public List<EsProcurementPlan> selectEsProcurementPlanList(EsProcurementPlan esProcurementPlan)
    {
        return esProcurementPlanMapper.selectEsProcurementPlanList(esProcurementPlan);
    }

    /**
     * 新增采购计划(es_procurement_plan)
     *
     * @param esProcurementPlan 采购计划(es_procurement_plan)
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEsProcurementPlan(EsProcurementPlan esProcurementPlan) {
        esProcurementPlan.setCreateTime(DateUtils.getNowDate());
        int rows = esProcurementPlanMapper.insertEsProcurementPlan(esProcurementPlan);

        // 保存采购明细
        if (esProcurementPlan.getProductList() != null && !esProcurementPlan.getProductList().isEmpty()) {
            esProcurementPlan.getProductList().forEach(product -> {
                product.setPlanId(esProcurementPlan.getId());
            });
            esProcurementPlanMapper.batchInsertPlanProduct(esProcurementPlan.getProductList());
        }

        return rows;
    }

    /**
     * 修改采购计划(es_procurement_plan)
     *
     * @param esProcurementPlan 采购计划(es_procurement_plan)
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEsProcurementPlan(EsProcurementPlan esProcurementPlan) {
        // 先删除原有的采购明细
        esProcurementPlanMapper.deletePlanProductByPlanId(esProcurementPlan.getId());

        // 保存新的采购明细
        if (esProcurementPlan.getProductList() != null && !esProcurementPlan.getProductList().isEmpty()) {
            esProcurementPlan.getProductList().forEach(product -> {
                product.setPlanId(esProcurementPlan.getId());
            });
            esProcurementPlanMapper.batchInsertPlanProduct(esProcurementPlan.getProductList());
        }

        return esProcurementPlanMapper.updateEsProcurementPlan(esProcurementPlan);
    }

    /**
     * 批量删除采购计划(es_procurement_plan)
     *
     * @param ids 需要删除的采购计划(es_procurement_plan)主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEsProcurementPlanByIds(Integer[] ids)
    {
        esProcurementPlanMapper.deleteSystemDeptByIds(ids);
        return esProcurementPlanMapper.deleteEsProcurementPlanByIds(ids);
    }

    /**
     * 删除采购计划(es_procurement_plan)信息
     *
     * @param id 采购计划(es_procurement_plan)主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEsProcurementPlanById(Integer id)
    {
        esProcurementPlanMapper.deleteSystemDeptById(id);
        return esProcurementPlanMapper.deleteEsProcurementPlanById(id);
    }

    @Override
    public List<SystemsDept> selectSystemsDeptList() {
        return esProcurementPlanMapper.selectSystemsDeptList();
    }

    @Override
    public List<EsAdminUser> selectEsAdminUserList() {
        return esProcurementPlanMapper.selectEsAdminUserList();
    }

    /**
     * 新增部门信息
     *
     * @param esProcurementPlan 采购计划(es_procurement_plan)对象
     */
    public void insertSystemDept(EsProcurementPlan esProcurementPlan)
    {
        List<SystemsDept> systemDeptList = esProcurementPlan.getSystemDeptList();
        Integer id = esProcurementPlan.getId();
        if (StringUtils.isNotNull(systemDeptList))
        {
            List<SystemsDept> list = new ArrayList<SystemsDept>();
            for (SystemsDept systemDept : systemDeptList)
            {
                systemDept.setId(id);
                list.add(systemDept);
            }
            if (list.size() > 0)
            {
                esProcurementPlanMapper.batchSystemDept(list);
            }
        }
    }
    /**
     * 新增平台管理员信息
     *
     * @param esProcurementPlan 采购计划(es_procurement_plan)对象
     */
    public void insertEsAdminUser(EsProcurementPlan esProcurementPlan)
    {
        List<EsAdminUser> esAdminUserList = esProcurementPlan.getEsAdminUserList();
        Integer id = esProcurementPlan.getId();
        if (StringUtils.isNotNull(esAdminUserList))
        {
            List<EsAdminUser> list = new ArrayList<EsAdminUser>();
            for (EsAdminUser esAdminUser : esAdminUserList)
            {
                esAdminUser.setId(id);
                list.add(esAdminUser);
            }
            if (list.size() > 0)
            {
                esProcurementPlanMapper.batchEsAdminUser(list);
            }
        }
    }

    @Override
    public List<SystemsDept> selectDeptTree() {
        List<SystemsDept> depts = selectSystemsDeptList();
        // 假设你有一个方法可以将部门列表转换为树形结构
        return buildDeptTree(depts);
    }

    @Override
    public List<ErpSuppliers> selectErpSuppliersList() {
        return esProcurementPlanMapper.selectErpSuppliersList();
    }

    @Override
    public List<SystemsDept> selectDeptTreeById(Integer id) {
        return esProcurementPlanMapper.selectDeptTreeById(id);
    }

    @Override
    public List<EsAdminUser> selectEsAdminUserListById(Integer id) {
        return esProcurementPlanMapper.selectEsAdminUserListById(id);
    }

    @Override
    public ErpSuppliers selectErpSuppliersListById(Integer id) {
        return esProcurementPlanMapper.selectErpSuppliersListById(id);
    }

    private List<SystemsDept> buildDeptTree(List<SystemsDept> depts) {
        List<SystemsDept> tree = new ArrayList<>();
        for (SystemsDept dept : depts) {
            if (dept.getParentId() == 0) {
                tree.add(dept);
                buildChildren(dept, depts);
            }
        }
        return tree;
    }

    private void buildChildren(SystemsDept parent, List<SystemsDept> depts) {
        for (SystemsDept dept : depts) {
            if (dept.getParentId().equals(parent.getId())) {
                parent.getChildren().add(dept);
                buildChildren(dept, depts);
            }
        }
    }
    @Override
    public List<EsProcurementPlanProduct> selectPlanProductByPlanId(Integer planId) {
        return esProcurementPlanMapper.selectPlanProductByPlanId(planId);
    }
}
